package com.stockz.equity.charting.view.widget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;

import org.springframework.aop.target.dynamic.Refreshable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.display.screen.AbstractDisplayableScreenConfiguration;
import com.stockz.equity.charting.event.EventPublisher;
import com.stockz.equity.charting.event.RepaintStockChartPanelEvent;
import com.stockz.equity.charting.util.ContextUtil;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.toolbar.IndicatorToolBar;
import com.stockz.equity.charting.view.widget.custom.FilteredComboBox;
import com.stockz.equity.charting.view.widget.custom.FilteredComboBox.OnSelectionCallback;
import com.stockz.equity.pattern.Child;

@Component
public class ScreenNameComboBox 
extends FilteredComboBox 
implements OnSelectionCallback,Child,Refreshable,Ordered {
	private static final long serialVersionUID = 4829265837978401089L;

	@SuppressWarnings({ "rawtypes" })
	private List<Class> parentList = new ArrayList<Class>(1);
	private Set<AbstractDisplayableScreenConfiguration> screenSet = new HashSet<AbstractDisplayableScreenConfiguration>();
	
	@Autowired StockChartView stockChartView;
	
	public ScreenNameComboBox() {
		parentList.add(IndicatorToolBar.class);
		setOnSelectionCallback(this);
	}
	
	@Override
	public void refresh() {
		screenSet.clear();
		screenSet.addAll(ContextUtil.getApplicationContext().getBeansOfType(AbstractDisplayableScreenConfiguration.class).values());
		setModel(new DefaultComboBoxModel(screenSet.toArray()));
	}
	

	@Override
	public void onSelection(Object selectedItem) {
		AbstractDisplayableScreenConfiguration screen = 
			(AbstractDisplayableScreenConfiguration)getSelectedItem();
		stockChartView
		.getSelectedStockChart()
		.getStockChartPanel()
		.getStockChartPanelState()
		.getDisplayableSet()
		.add(screen);
		
		screen.setStockChartView(stockChartView);
		
		EventPublisher.publishEvent(
				new RepaintStockChartPanelEvent(
						stockChartView.getSelectedStockChart().getStockChartPanel()));
	}
	@Override
	public Collection<Class> getParents() {
		return parentList;
	}

	@Override
	public int getOrder() {
		return 0;
	}

	@Override
	public long getLastRefreshTime() {
		return 0;
	}
	@Override
	public long getRefreshCount() {
		return 0;
	}
}
